package com.fjnldx.mes.service.schedulingClasses;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjnldx.mes.domain.domainRes.Result;
import com.fjnldx.mes.domain.pojo.Scheduling;
import com.fjnldx.mes.domain.pojo.SchedulingClasses;
import com.fjnldx.mes.mapper.scheduling.SchedulingMapper;
import com.fjnldx.mes.mapper.schedulingClasses.SchedulingClassesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class SchedulingClassesService extends ServiceImpl<SchedulingClassesMapper, SchedulingClasses> {

    @Autowired
    private SchedulingClassesMapper schedulingClassesMapper;

    public Result addClasses(SchedulingClasses schedulingClasses) {
        Result checkResult = this.checkScheduling(schedulingClasses);
        if( checkResult.getCode() == -1){
            checkResult.setMsg("新增失败!");
            return checkResult;
        }
        baseMapper.insert(schedulingClasses);
        return Result.success("新增成功!");
    }

    private Result checkScheduling(SchedulingClasses schedulingClasses){
        Map<String, String> queryMap = new HashMap<>();
        queryMap.put("`group`", schedulingClasses.getGroup());
        queryMap.put("org", schedulingClasses.getOrg());
        queryMap.put("cj", schedulingClasses.getCj());
        queryMap.put("gongxu", schedulingClasses.getGongxu());
        QueryWrapper<SchedulingClasses> queryWrap = new QueryWrapper<> ();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 查询同一集团+组织+车间+工序有没有冲突的时间段数据
        // 冲突的时间段有4种情况;
        // 1. 新增的班次的开始时间在已有的班次的开始时间和结束时间内
        // 2. 新增的班次的结束时间在已有的班次的开始时间和结束时间内
        // 3. 已有的班次的开始时间在新增的班次的开始时间和结束时间内
        // 4. 已有的班次的结束时间在新增的班次的开始时间和结束时间内
        queryWrap.allEq(queryMap).between("start_time", schedulingClasses.getStartTime(), schedulingClasses.getEndTime());
        if(baseMapper.selectCount(queryWrap) > 0){
            return Result.error("已存在时间段内的班次!");
        }
        queryWrap.clear();
        queryWrap.allEq(queryMap).between("end_time", schedulingClasses.getStartTime(), schedulingClasses.getEndTime());
        if(baseMapper.selectCount(queryWrap) > 0){
            return Result.error("已存在时间段内的班次!");
        }
        queryWrap.clear();
        queryWrap.allEq(queryMap).apply(String.format("'%s' between start_time and end_time", dateTimeFormatter.format(schedulingClasses.getStartTime())));
        if(baseMapper.selectCount(queryWrap) > 0){
            return Result.error("已存在时间段内的班次!");
        }
        return Result.success(null);
    }

    public Result deleteClasses(ArrayList<Integer> ids) {
        for(Integer id : ids){
            baseMapper.deleteById(id);
        }
        return Result.success("删除成功!");
    }


    public Result modifyClasses(SchedulingClasses schedulingClasses) {
        Result checkResult = this.checkScheduling(schedulingClasses);
        if(checkResult.getCode() == -1){
            return checkResult;
        }else{
            if(schedulingClasses.getId() == null){
                return Result.error("id不能为空!");
            }
            baseMapper.updateById(schedulingClasses);
            return Result.success("修改成功!");
        }
    }


    public IPage<SchedulingClasses> queryClasses(SchedulingClasses schedulingClasses, Page page) {
        QueryWrapper<SchedulingClasses> queryWrapper = new QueryWrapper<>();
        if(schedulingClasses.getBc() != null && !schedulingClasses.getBc().isEmpty()){
            queryWrapper.eq("bc", schedulingClasses.getBc());
        }
        if(schedulingClasses.getGroup() != null && !schedulingClasses.getGroup().isEmpty()){
            queryWrapper.eq("group", schedulingClasses.getGroup());
        }
        if(schedulingClasses.getOrg() != null && !schedulingClasses.getOrg().isEmpty()){
            queryWrapper.eq("org", schedulingClasses.getOrg());
        }
        if(schedulingClasses.getCj() != null && !schedulingClasses.getCj().isEmpty()){
            queryWrapper.eq("cj", schedulingClasses.getCj() );
        }
        if(schedulingClasses.getGongxu() != null && !schedulingClasses.getGongxu().isEmpty()){
            queryWrapper.eq("gongxu", schedulingClasses.getGongxu());
        }
        if(schedulingClasses.getMonitor() != null && !schedulingClasses.getMonitor().isEmpty()){
            queryWrapper.eq("monitor", schedulingClasses.getMonitor());
        }
        if(schedulingClasses.getClassCoach() != null && !schedulingClasses.getClassCoach().isEmpty()){
            queryWrapper.eq("class_coach", schedulingClasses.getClassCoach());
        }
        if(schedulingClasses.getStartTime() != null){
            queryWrapper.ge("start_time", schedulingClasses.getStartTime());
        }
        if(schedulingClasses.getEndTime() != null){
            queryWrapper.le("end_time", schedulingClasses.getEndTime());
        }
        IPage<SchedulingClasses> resultPage = baseMapper.selectPage(page, queryWrapper);
        return resultPage;
    }
}
